/*
 * Copyright (c) 2018-2024 北京华恒盛世科技有限公司
 * QUICKPOOL is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *     http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
*/

#include "xdrbase.h"
#include "channel.h"
#include "api.h"

/**
 * @brief       API: send signal to job.
 *
 * @param[in]   signalreq    #1: signal job request key values, including
 *                               kill, stop, resume and requeue job.
 * @param[in]   master       #2: master(port and name) to be called
 *
 * @retval      not NULL     #1: succeed
 * @retval      NULL         #2: failed
 *
 * @note        When succeed, QuickPoolJobReply should be freed by the caller.
 *              For JS replied error, caller should handle it.
 */
struct QuickPoolJobReply *
client_signaljob(struct request *signalreq, struct poolMaster *master)
{
    if (NULL == signalreq || 0 == signalreq->number) {
        log_message(ERR, NOCLASS, "%s: Must specify signal job request.", __func__);
	return NULL;
    }

    struct auth auth;
    gf_set_auth(&auth, commandParams[AUTH_KIND].string_value);

    unsigned int size; // for calculating the size of the request.
    size = gf_xdrsize_request(signalreq);
    size += gf_xdrsize_auth(&auth);
    size += HEADER_LEN;

    char *request;
    request = (char *)calloc(size, sizeof(char));
    if (NULL == request) {

        log_message(ERR, NOCLASS, STR_FUNC_D_FAIL_M, __func__, "calloc", size);
        gf_xdr_free(gf_xdr_auth, (void *)&auth);
        quickpool_errno = ERROR_MEMORY;
	return NULL;
    }

    struct header tohdr;
    gf_init_header(&tohdr);
    tohdr.reqcode = BATCH_JOB_SIG;

    XDR xdrs;
    xdrmem_create(&xdrs, request, size, XDR_ENCODE);

    if (!gf_xdr_message(&xdrs, signalreq, &tohdr, &auth, gf_xdr_request, NULL)) {

        // fail to encode request.
        log_message(ERR, NOCLASS, STR_FUNC_FAIL, __func__, "gf_xdr_message");
        xdr_destroy(&xdrs);
        FREEUP(request);
        gf_xdr_free(gf_xdr_auth, (void *)&auth);
        quickpool_errno = ERROR_XDR;

        return NULL;
    }

    char *masterList;
    unsigned int port;
    unsigned int connTimeout;
    unsigned int recvTimeout;    
    if (NULL != master) { // client set master, let's call it directly.
        masterList = master->master;
        port = master->port;
        connTimeout = master->conntimeout;
        recvTimeout = master->recvtimeout;
    } else {
        masterList = qp_getmastername();
        if (NULL == masterList) {
            // JM do not know who is master, let's try g_masterlist.
            masterList = g_masterlist;
        }
        port = g_js_port;
        connTimeout = g_conntimeout;
        recvTimeout = g_recvtimeout;
    }

    char *reply;
    int cc, fd;
    struct header outhdr;
    cc = gf_callmaster(masterList, port, connTimeout, recvTimeout, g_connretries,
                       XDR_GETPOS(&xdrs), request, &outhdr, &reply, &fd);

    gf_xdr_free(gf_xdr_auth, (void *)&auth);
    xdr_destroy(&xdrs);
    FREEUP(request);
    if (0 < fd) {
        gf_chan_close(fd);
    }

    if (cc < 0) { // communication in trouble.

        FREEUP(reply);
        log_message(ERR, NOCLASS, STR_FUNC_S_FAIL, __func__, "gf_callmaster", masterList);
        return NULL;
    }

    quickpool_errno = outhdr.reqcode;

    struct QuickPoolJobReply *jobReply = NULL;
    jobReply = (struct QuickPoolJobReply *)calloc(1, sizeof(struct QuickPoolJobReply));
    if (NULL == jobReply) {

        log_message(ERR, NOCLASS, STR_FUNC_D_FAIL_M, __func__,
                    "calloc", sizeof(struct QuickPoolJobReply));
        FREEUP(reply);
        return NULL;
    }

    // got job reply from JS, decode it.
    xdrmem_create(&xdrs, reply, outhdr.length, XDR_DECODE);
    if (!libxdr_job_reply(&xdrs, jobReply, &outhdr, NULL)) {

        log_message(ERR, NOCLASS, STR_FUNC_FAIL, __func__, "libxdr_job_reply");
        quickpool_errno = ERROR_XDR;
        xdr_destroy(&xdrs);
        FREEUP(reply);
        return NULL;
    }

    xdr_destroy(&xdrs);
    FREEUP(reply);

    return (jobReply); 
} // end function client_signaljob
